package com.calculator.demo;

/**
 * @description: 计算命令，每条命令代表一条计算指令
 * @author: YanXun
 * @date: 2023/3/6
 **/
public class CalculatorCommand extends Command {

    private Calculator calculator;
    private char operator;
    private int operand;

    CalculatorCommand(Calculator calculator, char operator, int operand) {
        super();
        this.calculator = calculator;
        this.operator = operator;
        this.operand = operand;
    }

    public Calculator getCalculator() {
        return calculator;
    }

    public void setCalculator(Calculator calculator) {
        this.calculator = calculator;
    }

    public char getOperator() {
        return operator;
    }

    public void setOperator(char operator) {
        this.operator = operator;
    }

    public int getOperand() {
        return operand;
    }

    public void setOperand(int operand) {
        this.operand = operand;
    }

    @Override
    public void execute() {
        calculator.operation(operator, operand);

    }

    @Override
    public void unexecute() {
        calculator.operation(undoOperator(operator), operand);

    }

    /**
     * 反向补偿操作
     * @param operator2
     * @return
     */
    private char undoOperator(char operator2) {
        char ch = 0;
        switch(operator2) {
            case '+':ch='-';break;
            case '-':ch='+';break;
            case '*':ch='/';break;
            case '/':ch='*';break;
        }
        return ch;
    }
}

